/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Description: MemMmap class test.
 */
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "common.h"
#include "datasystem/common/shared_memory/mmap/allocation.h"
#include "datasystem/common/shared_memory/mmap/mem_mmap.h"
#include "../../../../common/binmock/binmock.h"

using namespace ::testing;

namespace datasystem {
namespace ut {
class MemMmapTest : public CommonTest {
};

TEST_F(MemMmapTest, InitFailed)
{
    memory::MemMmap m;
    size_t sz = 1024;
    // init with huge
    DS_ASSERT_NOT_OK(m.Initialize(sz, true, true));
    BINEXPECT_CALL(&ftruncate, (_, _)).WillOnce(Return(-1));
    DS_ASSERT_NOT_OK(m.Initialize(sz));
    RELEASE_STUBS
    BINEXPECT_CALL(&mmap, (_, _, _, _, _, _)).WillOnce(Return(MAP_FAILED));
    DS_ASSERT_NOT_OK(m.Initialize(sz));

    // In order to pass the coverage.
    memory::Allocation alloc;
    (void)alloc;
}

TEST_F(MemMmapTest, CommitFailed)
{
    memory::MemMmap m;
    size_t sz = 1024;
    DS_ASSERT_OK(m.Initialize(sz));
    BINEXPECT_CALL(&fallocate, (_, _, _, _)).WillRepeatedly(Return(-1));
    ASSERT_TRUE(m.Commit(m.Pointer(), 0, 1));
}
}  // namespace ut
}  // namespace datasystem